<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>poetry 后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" type="image/ico" href="/images/logo.png"/>

    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/css/common.css" media="all">
    <link rel="stylesheet" href="/css/bly-icon.css" media="all">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div th:replace="user/common::headerhtml"></div>
    <!--    <div th:replace="user/common::menuhtml"></div>-->
    <div class="layui-container">
        <!-- 内容主体区域 -->
        <div style="padding-top: 15px;">
            <div class="bly-panel" style="padding: 15px;">
                <div class="layui-btn-group demoTable">
                    <!--<button class="layui-btn" data-type="getCheckData">获取选中行数据</button>-->
                </div>
                <table class="layui-hide" id="table" lay-filter="table"></table>
            </div>

        </div>
    </div>

    <div th:replace="user/common::footerhtml"></div>
</div>
<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/layui/menu.js" charset="utf-8"></script>
<script>
    //JavaScript代码区域
    layui.use(['element', 'layer', 'table'], function () {
        var element = layui.element,
            $ = layui.jquery,
            table = layui.table;
        table.render({
            elem: '#table'
            , id: "dataTable"
            , url: '/discuss/pageList'
            , method: 'post'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'discussName', title: '评论者'}
                    , {field: 'email', title: '邮箱'}
                    , {field: 'content', title: '评论内容', width: 300}
                    , {field: 'discussTime', title: '评论时间'}
                    , {field: 'praiseNum', title: '获赞数'}
                    , {field: 'ip', title: 'IP地址'}
                    , {fixed: 'right', width: 178, align: 'center', toolbar: '#barDemo'}
                ]
            ]
            , page: true
            , response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
        });

        //监听行工具事件
        table.on('tool(table)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'reply') {
                layer.prompt({
                    title: "回复游客：" + data.discussName,
                    formType: 2
                }, function (e, a) {
                    $.ajax({
                        url: '/discuss/save',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            articleId: data.articleId,
                            discussName: '作者',
                            content: e,
                            replyId: data.id,
                            isAdmin:'true'
                        },
                        success: function (res) {
                            if (res.status != 200) {
                                layer.alert(res.msg, {icon: 5});
                                return;
                            }
                            layer.msg("评论成功");
                        }
                    });
                    layer.close(a)
                })
            } else if (layEvent === 'del') {
                layer.confirm('真的删除么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        url: '/discuss/del',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            id: data.id
                        },
                        success: function (res) {
                            layer.msg("删除成功");
                        }
                    });
                });
            }
        });


    });

</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="reply">回复</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
